﻿using System;
using System.Web;
using DotNetNuke.UI.Skins;

namespace HeroldIT.Dnn.Modules.Compatibility
{
    /// <summary>
    /// Provides a compatibility layer for the
    /// <see cref="DotNetNuke.Entities.Portals.PortalSettings"/> class.
    /// </summary>
    public abstract class PortalSettings
    {
        #region constants

        private static readonly string PortalSettingsItemKey = typeof (PortalSettings).FullName + "_Instance";

        /// <summary>
        /// Defines the default portal skin source.
        /// </summary>
        protected const string DefaultPortalSkinSrc = "[G]Skins/MinimalExtropy/index.ascx";

        #endregion

        #region static properties

        private static readonly object instanceLock = new object();

        /// <summary>
        /// Gets an instance of the PortalSettings compatibility class.
        /// </summary>
        public static PortalSettings Instance
        {
            get
            {
                if (null == HttpContext.Current.Items[PortalSettingsItemKey])
                {
                    lock (instanceLock)
                    {
                        if (null == HttpContext.Current.Items[PortalSettingsItemKey])
                        {
                            HttpContext.Current.Items[PortalSettingsItemKey] =
                                Environment.DotNetNukeVersion.Major < 5
                                    ? new Dnn4.PortalSettings()
                                    : AppDomain.CurrentDomain.CreateInstanceAndUnwrap(
                                          "HeroldIT.Dnn.Modules.Compatibility.Dnn5",
                                          "HeroldIT.Dnn.Modules.Compatibility.Dnn5.PortalSettings");
                        }
                    }
                }
                return (PortalSettings) HttpContext.Current.Items[PortalSettingsItemKey];
            }
        }

        #endregion

        #region static methods

        /// <summary>
        /// Gets the source of the skin, or the default skin source, if no skin is selected.
        /// </summary>
        /// <param name="skin">The selected skin.</param>
        /// <returns>The source of the skin, or the default skin source, if no skin is selected.</returns>
        protected static string GetSafeSkinSrc(SkinInfo skin)
        {
            return skin == null ? DefaultPortalSkinSrc : skin.SkinSrc;
        }

        #endregion

        #region abstract properties

        /// <summary>
        /// Gets the path the the default admin skin.
        /// </summary>
        public abstract string DefaultAdminSkin { get; }

        /// <summary>
        /// Gets the path the the default admin container.
        /// </summary>
        public abstract string DefaultAdminContainer { get; }

        /// <summary>
        /// Gets the path the the default portal skin.
        /// </summary>
        public abstract string DefaultPortalSkin { get; }

        /// <summary>
        /// Gets the path the the default portal container.
        /// </summary>
        public abstract string DefaultPortalContainer { get; }

        #endregion
    }
}
